home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / copy.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  10.6 KB  |  438 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import types
  5. from copy_reg import dispatch_table
  6.  
  7. class Error(Exception):
  8.     pass
  9.  
  10. error = Error
  11.  
  12. try:
  13.     from org.python.core import PyStringMap
  14. except ImportError:
  15.     PyStringMap = None
  16.  
  17. __all__ = [
  18.     'Error',
  19.     'copy',
  20.     'deepcopy']
  21.  
  22. def copy(x):
  23.     cls = type(x)
  24.     copier = _copy_dispatch.get(cls)
  25.     if copier:
  26.         return copier(x)
  27.     
  28.     copier = getattr(cls, '__copy__', None)
  29.     if copier:
  30.         return copier(x)
  31.     
  32.     reductor = dispatch_table.get(cls)
  33.     if reductor:
  34.         rv = reductor(x)
  35.     else:
  36.         reductor = getattr(x, '__reduce_ex__', None)
  37.         if reductor:
  38.             rv = reductor(2)
  39.         else:
  40.             reductor = getattr(x, '__reduce__', None)
  41.             if reductor:
  42.                 rv = reductor()
  43.             else:
  44.                 raise Error('un(shallow)copyable object of type %s' % cls)
  45.     return _reconstruct(x, rv, 0)
  46.  
  47. _copy_dispatch = d = { }
  48.  
  49. def _copy_atomic(x):
  50.     return x
  51.  
  52. d[types.NoneType] = _copy_atomic
  53. d[types.IntType] = _copy_atomic
  54. d[types.LongType] = _copy_atomic
  55. d[types.FloatType] = _copy_atomic
  56. d[types.BooleanType] = _copy_atomic
  57.  
  58. try:
  59.     d[types.ComplexType] = _copy_atomic
  60. except AttributeError:
  61.     pass
  62.  
  63. d[types.StringType] = _copy_atomic
  64.  
  65. try:
  66.     d[types.UnicodeType] = _copy_atomic
  67. except AttributeError:
  68.     pass
  69.  
  70.  
  71. try:
  72.     d[types.CodeType] = _copy_atomic
  73. except AttributeError:
  74.     pass
  75.  
  76. d[types.TypeType] = _copy_atomic
  77. d[types.XRangeType] = _copy_atomic
  78. d[types.ClassType] = _copy_atomic
  79. d[types.BuiltinFunctionType] = _copy_atomic
  80.  
  81. def _copy_list(x):
  82.     return x[:]
  83.  
  84. d[types.ListType] = _copy_list
  85.  
  86. def _copy_tuple(x):
  87.     return x[:]
  88.  
  89. d[types.TupleType] = _copy_tuple
  90.  
  91. def _copy_dict(x):
  92.     return x.copy()
  93.  
  94. d[types.DictionaryType] = _copy_dict
  95. if PyStringMap is not None:
  96.     d[PyStringMap] = _copy_dict
  97.  
  98.  
  99. def _copy_inst(x):
  100.     if hasattr(x, '__copy__'):
  101.         return x.__copy__()
  102.     
  103.     if hasattr(x, '__getinitargs__'):
  104.         args = x.__getinitargs__()
  105.         y = x.__class__(*args)
  106.     else:
  107.         y = _EmptyClass()
  108.         y.__class__ = x.__class__
  109.     if hasattr(x, '__getstate__'):
  110.         state = x.__getstate__()
  111.     else:
  112.         state = x.__dict__
  113.     if hasattr(y, '__setstate__'):
  114.         y.__setstate__(state)
  115.     else:
  116.         y.__dict__.update(state)
  117.     return y
  118.  
  119. d[types.InstanceType] = _copy_inst
  120. del d
  121.  
  122. def deepcopy(x, memo = None, _nil = []):
  123.     if memo is None:
  124.         memo = { }
  125.     
  126.     d = id(x)
  127.     y = memo.get(d, _nil)
  128.     if y is not _nil:
  129.         return y
  130.     
  131.     cls = type(x)
  132.     copier = _deepcopy_dispatch.get(cls)
  133.     if copier:
  134.         y = copier(x, memo)
  135.     else:
  136.         
  137.         try:
  138.             issc = issubclass(cls, type)
  139.         except TypeError:
  140.             issc = 0
  141.  
  142.         if issc:
  143.             y = _deepcopy_atomic(x, memo)
  144.         else:
  145.             copier = getattr(x, '__deepcopy__', None)
  146.             if copier:
  147.                 y = copier(memo)
  148.             else:
  149.                 reductor = dispatch_table.get(cls)
  150.                 if reductor:
  151.                     rv = reductor(x)
  152.                 else:
  153.                     reductor = getattr(x, '__reduce_ex__', None)
  154.                     if reductor:
  155.                         rv = reductor(2)
  156.                     else:
  157.                         reductor = getattr(x, '__reduce__', None)
  158.                         if reductor:
  159.                             rv = reductor()
  160.                         else:
  161.                             raise Error('un(deep)copyable object of type %s' % cls)
  162.                 y = _reconstruct(x, rv, 1, memo)
  163.     memo[d] = y
  164.     _keep_alive(x, memo)
  165.     return y
  166.  
  167. _deepcopy_dispatch = d = { }
  168.  
  169. def _deepcopy_atomic(x, memo):
  170.     return x
  171.  
  172. d[types.NoneType] = _deepcopy_atomic
  173. d[types.IntType] = _deepcopy_atomic
  174. d[types.LongType] = _deepcopy_atomic
  175. d[types.FloatType] = _deepcopy_atomic
  176. d[types.BooleanType] = _deepcopy_atomic
  177.  
  178. try:
  179.     d[types.ComplexType] = _deepcopy_atomic
  180. except AttributeError:
  181.     pass
  182.  
  183. d[types.StringType] = _deepcopy_atomic
  184.  
  185. try:
  186.     d[types.UnicodeType] = _deepcopy_atomic
  187. except AttributeError:
  188.     pass
  189.  
  190.  
  191. try:
  192.     d[types.CodeType] = _deepcopy_atomic
  193. except AttributeError:
  194.     pass
  195.  
  196. d[types.TypeType] = _deepcopy_atomic
  197. d[types.XRangeType] = _deepcopy_atomic
  198. d[types.ClassType] = _deepcopy_atomic
  199. d[types.BuiltinFunctionType] = _deepcopy_atomic
  200.  
  201. def _deepcopy_list(x, memo):
  202.     y = []
  203.     memo[id(x)] = y
  204.     for a in x:
  205.         y.append(deepcopy(a, memo))
  206.     
  207.     return y
  208.  
  209. d[types.ListType] = _deepcopy_list
  210.  
  211. def _deepcopy_tuple(x, memo):
  212.     y = []
  213.     for a in x:
  214.         y.append(deepcopy(a, memo))
  215.     
  216.     d = id(x)
  217.     
  218.     try:
  219.         return memo[d]
  220.     except KeyError:
  221.         pass
  222.  
  223.     for i in range(len(x)):
  224.         if x[i] is not y[i]:
  225.             y = tuple(y)
  226.             break
  227.             continue
  228.     else:
  229.         y = x
  230.     memo[d] = y
  231.     return y
  232.  
  233. d[types.TupleType] = _deepcopy_tuple
  234.  
  235. def _deepcopy_dict(x, memo):
  236.     y = { }
  237.     memo[id(x)] = y
  238.     for key, value in x.iteritems():
  239.         y[deepcopy(key, memo)] = deepcopy(value, memo)
  240.     
  241.     return y
  242.  
  243. d[types.DictionaryType] = _deepcopy_dict
  244. if PyStringMap is not None:
  245.     d[PyStringMap] = _deepcopy_dict
  246.  
  247.  
  248. def _keep_alive(x, memo):
  249.     
  250.     try:
  251.         memo[id(memo)].append(x)
  252.     except KeyError:
  253.         memo[id(memo)] = [
  254.             x]
  255.  
  256.  
  257.  
  258. def _deepcopy_inst(x, memo):
  259.     if hasattr(x, '__deepcopy__'):
  260.         return x.__deepcopy__(memo)
  261.     
  262.     if hasattr(x, '__getinitargs__'):
  263.         args = x.__getinitargs__()
  264.         args = deepcopy(args, memo)
  265.         y = x.__class__(*args)
  266.     else:
  267.         y = _EmptyClass()
  268.         y.__class__ = x.__class__
  269.     memo[id(x)] = y
  270.     if hasattr(x, '__getstate__'):
  271.         state = x.__getstate__()
  272.     else:
  273.         state = x.__dict__
  274.     state = deepcopy(state, memo)
  275.     if hasattr(y, '__setstate__'):
  276.         y.__setstate__(state)
  277.     else:
  278.         y.__dict__.update(state)
  279.     return y
  280.  
  281. d[types.InstanceType] = _deepcopy_inst
  282.  
  283. def _reconstruct(x, info, deep, memo = None):
  284.     if isinstance(info, str):
  285.         return x
  286.     
  287.     if memo is None:
  288.         memo = { }
  289.     
  290.     n = len(info)
  291.     (callable, args) = info[:2]
  292.     if n > 2:
  293.         state = info[2]
  294.     else:
  295.         state = { }
  296.     if n > 3:
  297.         listiter = info[3]
  298.     else:
  299.         listiter = None
  300.     if n > 4:
  301.         dictiter = info[4]
  302.     else:
  303.         dictiter = None
  304.     if deep:
  305.         args = deepcopy(args, memo)
  306.     
  307.     y = callable(*args)
  308.     memo[id(x)] = y
  309.     if listiter is not None:
  310.         for item in listiter:
  311.             if deep:
  312.                 item = deepcopy(item, memo)
  313.             
  314.             y.append(item)
  315.         
  316.     
  317.     if dictiter is not None:
  318.         for key, value in dictiter:
  319.             if deep:
  320.                 key = deepcopy(key, memo)
  321.                 value = deepcopy(value, memo)
  322.             
  323.             y[key] = value
  324.         
  325.     
  326.     if state:
  327.         if deep:
  328.             state = deepcopy(state, memo)
  329.         
  330.         if hasattr(y, '__setstate__'):
  331.             y.__setstate__(state)
  332.         elif isinstance(state, tuple) and len(state) == 2:
  333.             (state, slotstate) = state
  334.         else:
  335.             slotstate = None
  336.         if state is not None:
  337.             y.__dict__.update(state)
  338.         
  339.         if slotstate is not None:
  340.             for key, value in slotstate.iteritems():
  341.                 setattr(y, key, value)
  342.             
  343.         
  344.     
  345.     return y
  346.  
  347. del d
  348. del types
  349.  
  350. class _EmptyClass:
  351.     pass
  352.  
  353.  
  354. def _test():
  355.     l = [
  356.         None,
  357.         1,
  358.         0x2L,
  359.         3.1400000000000001,
  360.         'xyzzy',
  361.         (1, 0x2L),
  362.         [
  363.             3.1400000000000001,
  364.             'abc'],
  365.         {
  366.             'abc': 'ABC' },
  367.         (),
  368.         [],
  369.         { }]
  370.     l1 = copy(l)
  371.     print l1 == l
  372.     l1 = map(copy, l)
  373.     print l1 == l
  374.     l1 = deepcopy(l)
  375.     print l1 == l
  376.     
  377.     class C:
  378.         
  379.         def __init__(self, arg = None):
  380.             self.a = 1
  381.             self.arg = arg
  382.             if __name__ == '__main__':
  383.                 import sys
  384.                 file = sys.argv[0]
  385.             else:
  386.                 file = __file__
  387.             self.fp = open(file)
  388.             self.fp.close()
  389.  
  390.         
  391.         def __getstate__(self):
  392.             return {
  393.                 'a': self.a,
  394.                 'arg': self.arg }
  395.  
  396.         
  397.         def __setstate__(self, state):
  398.             for key, value in state.iteritems():
  399.                 setattr(self, key, value)
  400.             
  401.  
  402.         
  403.         def __deepcopy__(self, memo = None):
  404.             new = self.__class__(deepcopy(self.arg, memo))
  405.             new.a = self.a
  406.             return new
  407.  
  408.  
  409.     c = C('argument sketch')
  410.     l.append(c)
  411.     l2 = copy(l)
  412.     print l == l2
  413.     print l
  414.     print l2
  415.     l2 = deepcopy(l)
  416.     print l == l2
  417.     print l
  418.     print l2
  419.     l.append({
  420.         l[1]: l,
  421.         'xyz': l[2] })
  422.     l3 = copy(l)
  423.     import repr
  424.     print map(repr.repr, l)
  425.     print map(repr.repr, l1)
  426.     print map(repr.repr, l2)
  427.     print map(repr.repr, l3)
  428.     l3 = deepcopy(l)
  429.     import repr
  430.     print map(repr.repr, l)
  431.     print map(repr.repr, l1)
  432.     print map(repr.repr, l2)
  433.     print map(repr.repr, l3)
  434.  
  435. if __name__ == '__main__':
  436.     _test()
  437.  
  438.